﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Easily
{
    public partial class frmUEcomRecv : Form
    {
        #region 对外的属性和私有成员
        /// <summary>
        /// 存储传入的图标控件
        /// </summary>
        private GeneralIcon MyIcon = null;
        #endregion

        #region 构造函数和对外的方法
        /// <summary>
        /// 类的构造函数
        /// 功能概要：保存控件对象；设置默认延时1000毫秒
        /// </summary>
        /// <param name="icon"></param>
        public frmUEcomRecv(GeneralIcon icon)
        {
            InitializeComponent();
            this.MyIcon = icon;
            this.createCode();
        }
        #endregion

        #region 事件
        /// <summary>
        /// 点击“确定”按钮
        /// 功能概要：设置延时
        /// </summary>
        private void buttonEnsure_Click(object sender, EventArgs e)
        {
            this.createCode();
            this.DialogResult = DialogResult.OK;//表示当窗体用作对话框时该窗体的结果，
            this.Close();
        }
        
        #endregion
        #region 内部函数
        /// <summary>
        /// 创建生成的代码
        /// </summary>
        private void createCode()
        {
            try
            {
                //添加头文件、初始化代码等
                MyIcon.code.Clear();//清空以前代码
                MyIcon.code.HeadFile = "#include \"wifi.h\""; //添加构件的头文件
                MyIcon.code.Comment = "接收到WIFI数据";
                //MyIcon.code.GlobalVariable = "uint8_t* jUEcomRecv;\r\nuint16_t jUEcomRecv_len;";
                MyIcon.code.InterruptEnable = "";
                MyIcon.code.InterruptServiceStart =
                    //[2024/1/18]
                    "\tuint8_t " + "ch;//记录接收到的数据字节数\r\n"
                    + "\tuint8_t " + "flag;//记录接收到的数据字节数\r\n"
                    + "\tch = uart_re1(UART_UE, &flag);   //调用接收一个字节的函数，清接收中断位\r\n"
                    + "\t//-------------------------------\r\n"
                    + "\t//接收到一个字节\r\n"
                    + "\tif(!flag) goto UART_UE_Handler_Exit;\r\n"
                    + "\tif(gcRecvLen == 0)\r\n"
                        + "\t\twifi_int_re(ch, &gcRecvLen, gcRecvBuf);\r\n"
                    + "\telse\r\n"
                        + "\t\tgoto UART_UE_Handler_Exit;\r\n"
                    + "\t//未接收到服务器发来的数据，退出\r\n"
                    + "\tif(gcRecvLen == 0) goto UART_UE_Handler_Exit1;\r\n"
                    + "\t//已经收到一帧字节数为gcRecvLen，在gcRecvBuf中\r\n"
                    + "\tswitch(gcRecvBuf[16])  //帧标识\r\n"
                    + "\t{\r\n"
                        + "\t\t//若为User串口程序更新命令，则进行程序更新\r\n"
                            + "\t\tcase \'T\':\r\n"
                                + "\t\t\tSYSTEM_FUNCTION((uint8_t *)(gcRecvBuf+17));\r\n"
                                + "\t\t\tgcRecvLen = 0;         //恢复接收状态\r\n"
                            + "\t\tbreak;\r\n"
                        + "\t//若为复位命令，则进行程序更新\r\n"
                            + "\t\tcase \'R\':\r\n"
                                + "\t\t\tNVIC_SystemReset();\r\n"
                            + "\t\tbreak;\r\n"
                            + "\t\tdefault:\r\n"
                                + "\t\t\tgoto UART_UE_Handler_Exit1;\r\n"
                            + "\t\tbreak;\r\n"
                    + "\t}\r\n"
                    + "\t//其它网络数据\r\n"
                    + "\tif (gcRecvLen>0)\r\n"
                    + "\t{\r\n";
                MyIcon.code.InterruptServiceEnd = "\t}\r\n"
                    + "UART_UE_Handler_Exit:\r\n"
                    + "\tgcRecvLen = 0;\r\n"
                    + "\tUART_UE_Handler_Exit1:\r\n";
                //MyIcon.code.Initialization = "void uecom_recv_hander(uint16_t length,uint8_t* buf);\r\n\tsetUEcomRecv((uint32_t)(&uecom_recv_hander));";
                //MyIcon.code.Save = milliSecond.ToString();
            }
            catch { }
        }
        #endregion
    }
}
